<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class Messages {

	public $types = array('help', 'info','success', 'danger', 'warning' );
	public $short_types = array('h', 'i', 's', 'e', 'w');
	public $msgClass = 'alert';
	public $msgWrapper = '<div class="%s alert-%s">
						<button type="button" class="close" data-dismiss="alert">
							<span aria-hidden="true">&times;</span>
							<span class="sr-only">Close</span>
						</button>
						%s
					</div>';
	public $msgBefore = '<p>';
	public $msgAfter = "</p>\n";
	private $CI;
	
	function __construct($params = array()) {
		$this->CI = & get_instance();
		$this->CI->load->library('session');
	}
	
	function clear($type = null) {
		if (!empty($type)) {
			$messages = $this->CI->session->userdata('messages');
			if (!is_array($messages)) {
				$messages = array();
			}
			
			if (array_key_exists($type, $messages)) {
				unset($messages[$type]);
			}
			
			$this->CI->session->set_userdata('messages', $messages);
		} else {
			$this->CI->session->set_userdata('messages', array());
		}
	}
	
	function add($message, $type = 'info') {
		$messages = $this->CI->session->userdata('messages');
		if( strlen(trim($type)) == 1 ) {
			$type = str_replace( $this->short_types, $this->types, $type );
		}
		if(!$type){
			$type = 'info';
		}
		if( !in_array($type, $this->types) )
		{
			die('"' . strip_tags($type) . '" is not a valid message type!' );
		}

		if (!is_array($messages)) {
			$messages = array();
		}
		
		if (is_a($message, 'Exception')) {
			$message = $message->getMessage();
			$type = 'error';
		}
		
		if ((!isset($messages[$type]) || !in_array($message, $messages[$type])) && is_string($message) && $message) {
			$messages[$type][] = $message;
		}
		
		$this->CI->session->set_userdata('messages', $messages);
	}
	
	function count($type = null) {
		if( strlen(trim($type)) == 1 ) {
			$type = str_replace( $this->short_types, $this->types, $type );
		}
		$messages = $this->CI->session->userdata('messages');
		if (!is_array($messages)) {
			$messages = array();
		}
		
		if (!empty($type)) {
			if (array_key_exists($type, $messages)) {
				return count($messages[$type]);
			} else {
				return 0;
			}
		}
		
		$i = 0;
		foreach ($messages as $type => $m) {
			$i += count($messages[$type]);
		}
		return $i;
	}
	
	function get($type = null) {
		if( strlen(trim($type)) == 1 ) {
			$type = str_replace( $this->short_types, $this->types, $type );
		}
		$messages = $this->CI->session->userdata('messages');
		if (!is_array($messages)) {
			$messages = array();
		}
		
		if (!empty($type)) {
			if (array_key_exists($type, $messages)) {
				$messages = $messages[$type];
				$this->clear($type);
				return $messages;
			} else {
				return array();
			}
		}
		
		$this->clear();
		return $messages;
	}
	public function display($type=null) {
		if( strlen(trim($type)) == 1 ) {
			$type = str_replace( $this->short_types, $this->types, $type );
		}

		if(!$this->count($type)) return '';
		$content = '';
		$data = '';
		$messages = $this->CI->session->userdata('messages');
		if (!is_array($messages)) {
			$messages = array();
		}

		// Print a certain type of message?
		if (!empty($type)) {
			if (array_key_exists($type, $messages)) {
				foreach($messages[$type] as $msg) {
					$content .= $this->msgBefore . $msg . $this->msgAfter;
				}
				$data .= sprintf($this->msgWrapper, $this->msgClass, $type, $content);
				$this->clear($type);
			}
		// Print ALL queued messages
		} else {
			foreach( $messages as $type => $msgArray ) {
				$content = '';
				foreach( $msgArray as $msg ) {
					$content .= $this->msgBefore . $msg . $this->msgAfter;
				}
				$data .= sprintf($this->msgWrapper, $this->msgClass, $type, $content);
			}
			
			// Clear ALL of the messages
			$this->clear();
		
		// Invalid Message Type?
		}
		return $data;
	}
}  